package com.mmall.controller.basecontroller;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import com.sun.org.apache.bcel.internal.generic.IUSHR;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/11/9.
 */
public class CategoryBaseController {

    @Autowired
    private IUserService iUserService;

    /**
     * 验证当前登陆用户是否为管理员
     *
     * @param httpSession
     * @return
     */
    public ServerResponse checkAdmin(HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(Const.CURRENT_USER);
//        判断是否登陆
        if (user == null) {
            return ServerResponse.createByErrocodeMessage(ResponseCode.ERROR.getCode(), "用户未登录");
        }
//        判断是否为管理员
        if (iUserService.checkAdminRole(user).isSuccess()) {
            return ServerResponse.createBySuccess();
        } else {
            return ServerResponse.createByErrorMessage("无权操作，需要管理员权限");
        }
    }
    
}
